<template>
  <div class="login-container" style="z-index: 9">
    <el-card class="login-box">
      <h1 class="title">欢迎回来!!!!!</h1>
      <el-divider />

      <el-form
        ref="loginForm"
        style="max-width: 600px"
        :model="loginForm"
        :rules="rules"
        label-width="auto"
        class="demo-loginForm"
        :size="formSize"
      >
        <!-- 用户名输入 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
            placeholder="用户名"
            size="large"
            clearable
          />
        </el-form-item>

        <!-- 密码输入 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            type="password"
            placeholder="密码"
            size="large"
            show-password
          />
        </el-form-item>

        <!-- 登录按钮 -->
        <el-button
          type="primary"
          size="large"
          class="login-btn"
          @click="handleLogin('loginForm')"
        >
          登录
        </el-button>
      </el-form>
      <el-link type="primary" @click="goToRegister">没有账号? 注册</el-link>
    </el-card>
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rememberMe: false,
      formSize: "default",
    };
  },
  computed: {
    rules() {
      return {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      };
    },
  },
  methods: {
    ...mapActions("user", ["userLogin", "loadUserList"]),
    ...mapActions("supplier", ["supplierLogin", "loadSupplierList"]),
    async handleLogin(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            // 执行登录验证
            await this.userLogin({
              username: this.loginForm.username,
              password: this.loginForm.password,
            });
            // 登录成功跳转
            this.$router.push("/store");
          } catch (error) {
            this.handleSupplierLogin(formName);
          }
        } else {
          return false;
        }
      });
    },
    goToRegister() {
      this.$router.push("/register");
    },
    async handleSupplierLogin(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            // 执行登录验证
            await this.supplierLogin({
              username: this.loginForm.username,
              password: this.loginForm.password,
            });

            // 登录成功跳转
            this.$router.push("/productManagement");
          } catch (error) {
            this.$message.error("登录失败");
          }
        } else {
          return false;
        }
      });
    },
  },
  mounted() {
    // 优先读取 localStorage 中的登录信息
    const loginPerson = JSON.parse(localStorage.getItem("loginPerson"));
    if (loginPerson) {
      // 如果有登录信息，直接跳转到 HomeView
      this.$store.commit("SET_LOGIN_PERSON", loginPerson);
      this.$router.push("/home");
    } else {
      this.loadUserList();
      this.loadSupplierList();
    }
  },
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  background-image: url("@/assets/images/login-bg.png");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
.login-box {
  width: 400px;
  padding: 40px 30px;
  border-radius: 20px;
  z-index: 2;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 0.6);
  animation: cardEnter 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94) both;
  transform-origin: center;
}
@keyframes cardEnter {
  0% {
    opacity: 0;
    transform: translateY(20px) scale(0.95);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
.title {
  text-align: center;
  color: #2c3e50;
  margin: 0 0 20px;
  font-size: 28px;
  font-weight: 600;
}
.flex-between {
  display: flex;
  justify-content: space-between;
  margin: 20px 0;
}
.login-btn {
  width: 100%;
  margin-top: 10px;
  font-size: 16px;
  letter-spacing: 2px;
  background: linear-gradient(135deg, #409eff 0%, #36a2ff 100%);
  border: none;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}
.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(64, 158, 255, 0.3);
}
@media (max-width: 768px) {
  .login-box {
    width: 90%;
    padding: 30px 20px;
  }
}
</style>
